package cn.sxt.yxl.common.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import jdk.nashorn.internal.ir.RuntimeNode.Request;

/**
 * Http请求的工具类
 * 
 * @author wangshSxt
 *
 */
public class HttpUtil
{
	public String getMethod(String urlStr,Map<String,String> headerMap,Map<String,String> paramMap){
		ConstatFinalUtil.LOGGER.info("-----getMethod-----");
		StringBuffer sb = new StringBuffer();
		if(urlStr.indexOf("?")==-1){
			urlStr+="?";
		}else{
			urlStr+="&";
		}
		for (Iterator iterator = paramMap.entrySet().iterator(); iterator.hasNext();) {
			Entry en = (Entry) iterator.next();
			String key = en.getKey()+"";
			String value=en.getValue()+"";
			urlStr+=key+"="+value+"&";
		}
		
		
		
		System.out.println(urlStr);
		try {
			//创建url
			URL url = new URL(urlStr);
			//打开一个连接
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			
			for (Iterator iterator = headerMap.entrySet().iterator(); iterator.hasNext();) {
				Entry he = (Entry) iterator.next();
				String key = he.getKey()+"";
				String value=he.getValue()+"";
				connection.setRequestProperty(key, value);
			}
			//创建输入流
			InputStream is = null;
			ConstatFinalUtil.LOGGER.info("-----状态码为:{}----",connection.getResponseCode());
			if(connection.getResponseCode()==200){
				is = connection.getInputStream();
			}else{
				is= connection.getErrorStream();
			}
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String line="";
			while((line=br.readLine())!=null){
				System.out.println(line);
				sb.append(line);
			}
		} catch (Exception e) {
		}
		return sb.toString();
	}
	
	/**
	 * post请求
	 */
	public String postMethod(String urlStr,Map<String,String> headerMap,Map<String,String> paramMap){
		ConstatFinalUtil.LOGGER.info("-----postMethod-----");
		StringBuffer sb = new StringBuffer();
		StringBuffer param = new StringBuffer();
		for (Iterator iterator = paramMap.entrySet().iterator(); iterator.hasNext();) {
			Entry en = (Entry) iterator.next();
			String key = en.getKey()+"";
			String value=en.getValue()+"";
			param.append(key+"="+value+"&");
		}
		
		
		
		System.out.println(urlStr);
		try {
			//创建url
			URL url = new URL(urlStr);
			//打开一个连接
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			//打开输入输出流的开关
			connection.setDoInput(true);
			connection.setDoOutput(true);
			for (Iterator iterator = headerMap.entrySet().iterator(); iterator.hasNext();) {
				Entry he = (Entry) iterator.next();
				String key = he.getKey()+"";
				String value=he.getValue()+"";
				connection.setRequestProperty(key, value);
			}
			
			//创建输出流 并且把参数信息传到页面
			OutputStream os = connection.getOutputStream();
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os));
			bw.write(param.toString());
			bw.flush();
			bw.close();
			
			//创建输入流
			InputStream is = null;
			ConstatFinalUtil.LOGGER.info("-----状态码为:{}----",connection.getResponseCode());
			if(connection.getResponseCode()==200){
				is = connection.getInputStream();
			}else{
				is= connection.getErrorStream();
			}
			BufferedReader br = new BufferedReader(new InputStreamReader(is));
			String line="";
			while((line=br.readLine())!=null){
				System.out.println(line);
				sb.append(line);
			}
			br.close();
		} catch (Exception e) {
		}
		return sb.toString();
	}
}
